package com.bitin.adorn.demo.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashSet;
import java.util.Set;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class User {
    @Id
    private Long id;
    private String name;

//    @JsonManagedReference
//    @OneToOne(cascade = CascadeType.ALL)
//    @JoinColumn(name = "account_id", referencedColumnName = "id", unique = true)
//    private Account account;

    //@JsonBackReference
    @OneToMany(
            cascade = CascadeType.ALL,
            fetch = FetchType.EAGER,
            orphanRemoval = true
    )
    @JoinColumn(name = "uid", referencedColumnName = "id")
    private Set<BankCard> cards = new HashSet<>();

    public User name(String name) {
        this.name = name;
        return this;
    }

//    public User income(BigDecimal income) {
//        this.account.income(income);
//        return this;
//    }

    public User bindCard(BankCard card) {
        this.cards.add(card);
        return this;
    }

    public User approve(){
        this.cards.forEach(BankCard::approve);
        return this;
    }
}
